﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProgrammerToolsKits.Interface;
using ProgrammerToolsKits.Comic.Interface;
using ProgrammerToolsKits.Comic.Modal;

namespace ProgrammerToolsKits.Comic.Content
{
    public class DirectEnumerator
    : IComicPageEnumerator
    {
        static System.Security.Cryptography.HMACMD5 md5 = new System.Security.Cryptography.HMACMD5();
        #region Private

        //---------------------------------------------------------------------------------------------------
        INetworkService                     service             = null;
        HtmlAgilityPack.HtmlDocument        document            = new HtmlAgilityPack.HtmlDocument();
        string                              source              = "";
        string                              baseSource          ="";
        //---------------------------------------------------------------------------------------------------
        //===================================================================================================
        internal DirectEnumerator(IComicVolume volume,INetworkService service)
        {
            source = volume.Source;
            baseSource = volume.Source;
            this.service = service;
        }
        //---------------------------------------------------------------------------------------------------
        //===================================================================================================
        #endregion

        #region Enumerator
        public IComicPage Current { get; private set; }
        object IEnumerator.Current => Current;

        public IComicBook Book { get; private set; }

        public IComicVolume Volume { get; private set; }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this;
        }

        public IEnumerator<IComicPage> GetEnumerator()
        {
            return this;
        }

        #endregion

        #region MoveNext
        private Task<string> LoadContent(string address)
        {
            return service.GetHtml( address );
        }
        private string GetNextNode(string content)
        {
            var span = GetSpan( content );
            return span.SelectSingleNode( "./a[4]" ).Attributes["href"].Value;
        }
        private HtmlAgilityPack.HtmlNode GetSpan(string content)
        {
            document.LoadHtml( content );
            var span = document.DocumentNode.SelectSingleNode( "/html/body/div[2]/div[1]/span" );
            return span;
        }
        private string GetPrevNode(string content)
        {
            var span = GetSpan( content );
            return span.SelectSingleNode( "//a[2]" ).Attributes["href"].Value;
        }
        #endregion

        public bool MoveNext()
        {
            var str = LoadContent( source ).GetAwaiter().GetResult();
            var href = GetNextNode( str );
            if ( href == "javascript:alert('已经是最后一页');" || href == "javascript:alert('已经是第一页');" )
                return false;
            source = href;
            Current = new ComicPage()
            {
                Source = source,
                Hash = Convert.ToBase64String( md5.ComputeHash( Encoding.UTF8.GetBytes( source ) ) )
            };
            return true;
        }

        public void Reset()
        {
            source = baseSource;
        }

        public void Dispose()
        {
        }

        public void Load(IComicVolume volume, IComicBook book)
        {
            Volume = volume;
            Book = book;
        }
    }
    public class CCGalleryPageEnumerator
    {
        public CCGalleryPageEnumerator(IComicVolume volume,INetworkService service)
        {
            Next = new DirectEnumerator( volume,service);
            Previous = new DirectEnumerator(volume,service);
        }
        public IComicPageEnumerator Next { get; private set; }
        public IComicPageEnumerator Previous { get; private set; }
    }
}
